package com.topera.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.topera.commonutils.JwtUtils;
import com.topera.commonutils.MD5;
import com.topera.educenter.entity.RegisterVo;
import com.topera.educenter.entity.UcenterMember;
import com.topera.educenter.mapper.UcenterMemberMapper;
import com.topera.educenter.service.UcenterMemberService;
import com.topera.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author puzhenjiang
 * @since 2020-06-05
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public String login(UcenterMember member) {
        String mobile = member.getMobile();//手机
        String password = member.getPassword();//密码


        if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)){
            throw new GuliException(20001,"登录失败");
        }

        password = MD5.encrypt(password);//先将密码进行加密处理

        //判断手机号是否正确
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        wrapper.eq("password",password);
        wrapper.eq("is_disabled",0);
        UcenterMember result =  baseMapper.selectOne(wrapper);
        if(result==null){
            throw new GuliException(20001,"手机号或密码错误,登录失败");
        }

        //使用JWT生成token
        String jwtToken = JwtUtils.getJwtToken(result.getId(),result.getNickname());

        return jwtToken;
    }

    @Override
    public void register(RegisterVo registerVo) {
        String code = registerVo.getCode();//手机验证码
        String mobile = registerVo.getMobile();//手机号
        String nickname = registerVo.getNickname();//昵称
        String password = registerVo.getPassword();//密码

        if(StringUtils.isEmpty(code) || StringUtils.isEmpty(mobile) || StringUtils.isEmpty(nickname) || StringUtils.isEmpty(password)){
            throw new GuliException(20001,"注册失败");
        }

        //验证 验证码是否有效
        //获取Redis中的验证码
        String redisCode = redisTemplate.opsForValue().get(mobile);
        //验证码失效或是不匹配
        if(StringUtils.isEmpty(redisCode) || !code.equals(redisCode)){
            throw new GuliException(20001,"验证码不正确");
        }

        //判断手机号是否重复  为什么不通过AJAX异步判断呢？
        UcenterMember member = new UcenterMember();
        member.setNickname(nickname);
        member.setMobile(mobile);
        member.setPassword(MD5.encrypt(password));
        member.setIsDisabled(false);
        member.setAvatar("http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoj0hHXhgJNOTSOFsS4uZs8x1ConecaVOB8eIl115xmJZcT4oCicvia7wMEufibKtTLqiaJeanU2Lpg3w/132");


        baseMapper.insert(member);

    }

    @Override
    public UcenterMember getByOpenid(String openid) {
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("openid", openid);

        UcenterMember member = baseMapper.selectOne(queryWrapper);
        return member;
    }


    @Override
    public int countRegister(String day) {
        return baseMapper.countRegister(day);
    }
}
